﻿using Microsoft.AspNetCore.Mvc;
using NXDO.WebAPI.Controllers;

namespace TDM.WebBase
{
    /// <summary>
    /// 加解密上传下载控制器
    /// </summary>
    public class EncryptUpDnldController : ApiBinaryController
    {
        /// <summary>
        /// 上传数据
        /// </summary>
        protected UploadData uploadData;

        protected override string GetSaveFileName(string name, bool nameIsMimeContentType)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 处理文件流
        /// </summary>
        /// <param name="streams"></param>
        /// <param name="names"></param>
        /// <param name="nameIsMimeContentType"></param>
        /// <returns></returns>
        protected override object SaveUploadStream(IEnumerable<Stream> streams, List<string> names, bool nameIsMimeContentType)
        {
            var fs = streams.First();
            byte[] byteArray = new byte[fs.Length];
            fs.Read(byteArray, 0, byteArray.Length);
            var enArray = AES_EnorDecrypt.AESEncrypt(byteArray, AES_EnorDecrypt.m_key, AES_EnorDecrypt.m_vector);

            List<Stream> newStreams = new List<Stream>();
            MemoryStream fs2 = new MemoryStream();
            fs2.Write(enArray, 0, enArray.Length);
            fs2.Position = 0;
            newStreams.Add(fs2);
            return base.SaveUploadStream(newStreams, names, nameIsMimeContentType);
        }

        /// <summary>
        /// 解密下载文件,需要实现GetDecpFile()方法
        /// </summary>
        /// <remarks>需要实现GetDecpFile()方法</remarks>
        /// <param name="name"></param>
        /// <returns></returns>
        protected override Stream GetDownloadStream(string name)
        {
            var filePath = GetDecpFile(name);
            return DecpStream(filePath);
        }

        /// <summary>
        /// 获得待解密的文件路径
        /// </summary>
        /// <param name="name"></param>
        /// <returns>文件路径</returns>
        protected virtual String GetDecpFile(string name)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 文件解密成流
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private Stream DecpStream(string filename)
        {
            var newArray = DecryptFileToByte(filename);
            MemoryStream decryptStream = new();
            if (newArray.Length <= 0) throw new Exception("解密出错");
            decryptStream.Write(newArray);
            decryptStream.Position = 0;
            return decryptStream;
        }

        /// <summary>
        /// 解析成字节
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private static byte[] DecryptFileToByte(string filename)
        {
            using (FileStream fs = new(filename, FileMode.Open, FileAccess.Read))
            {
                byte[] byteArray = new byte[fs.Length];
                fs.Read(byteArray, 0, byteArray.Length);
                var newArray = AES_EnorDecrypt.AESDecrypt(byteArray, AES_EnorDecrypt.m_key, AES_EnorDecrypt.m_vector);
                if (newArray.Length <= 0) throw new Exception("解密出错");
                return newArray;
            }
        }
    }
}
